load("@bazel_common//tools/maven:pom_file.bzl", "pom_file")
load("@rules_jvm_external//:defs.bzl", "java_export")
load("//publish:cel_version.bzl", "CEL_VERSION")

# Note: These targets must reference the build targets in `src` directly in
# order to properly generate the maven dependencies in pom.xml.

RUNTIME_TARGETS = [
    "//runtime/src/main/java/dev/cel/runtime",
    "//runtime/src/main/java/dev/cel/runtime:base",
    "//runtime/src/main/java/dev/cel/runtime:interpreter",
    "//runtime/src/main/java/dev/cel/runtime:runtime_helpers",
    "//runtime/src/main/java/dev/cel/runtime:unknown_attributes",
]

LITE_RUNTIME_TARGETS = [
    "//runtime/src/main/java/dev/cel/runtime:lite_runtime_android",
    "//runtime/src/main/java/dev/cel/runtime:lite_runtime_factory_android",
    "//runtime/src/main/java/dev/cel/runtime:lite_runtime_library_android",
    "//common/src/main/java/dev/cel/common:proto_ast_android",  # Note: included due to generated protos requiring protolite dependency
    "//common/src/main/java/dev/cel/common/values:proto_message_lite_value_provider_android",
    "//protobuf/src/main/java/dev/cel/protobuf:cel_lite_descriptor",
]

COMPILER_TARGETS = [
    "//parser/src/main/java/dev/cel/parser",
    "//parser/src/main/java/dev/cel/parser:parser_builder",
    "//parser/src/main/java/dev/cel/parser:unparser",
    "//checker/src/main/java/dev/cel/checker:checker",
    "//checker/src/main/java/dev/cel/checker:checker_builder",
    "//checker/src/main/java/dev/cel/checker:proto_type_mask",
    "//checker/src/main/java/dev/cel/checker:proto_expr_visitor",
    "//compiler/src/main/java/dev/cel/compiler",
    "//compiler/src/main/java/dev/cel/compiler:compiler_builder",
]

VALIDATOR_TARGETS = [
    "//validator/src/main/java/dev/cel/validator",
    "//validator/src/main/java/dev/cel/validator:validator_builder",
    "//validator/src/main/java/dev/cel/validator:ast_validator",
    "//validator/src/main/java/dev/cel/validator:validator_impl",
    "//validator/src/main/java/dev/cel/validator/validators:timestamp",
    "//validator/src/main/java/dev/cel/validator/validators:duration",
    "//validator/src/main/java/dev/cel/validator/validators:regex",
    "//validator/src/main/java/dev/cel/validator/validators:homogeneous_literal",
]

OPTIMIZER_TARGETS = [
    "//optimizer/src/main/java/dev/cel/optimizer",
    "//optimizer/src/main/java/dev/cel/optimizer:optimizer_builder",
    "//optimizer/src/main/java/dev/cel/optimizer:ast_optimizer",
    "//optimizer/src/main/java/dev/cel/optimizer:optimization_exception",
    "//optimizer/src/main/java/dev/cel/optimizer:mutable_ast",
    "//optimizer/src/main/java/dev/cel/optimizer:optimizer_impl",
    "//optimizer/src/main/java/dev/cel/optimizer/optimizers:constant_folding",
    "//optimizer/src/main/java/dev/cel/optimizer/optimizers:common_subexpression_elimination",
]

POLICY_COMPILER_TARGETS = [
    "//policy/src/main/java/dev/cel/policy:policy",
    "//policy/src/main/java/dev/cel/policy:source",
    "//policy/src/main/java/dev/cel/policy:validation_exception",
    "//policy/src/main/java/dev/cel/policy:parser_factory",
    "//policy/src/main/java/dev/cel/policy:yaml_parser",
    "//policy/src/main/java/dev/cel/policy:parser",
    "//policy/src/main/java/dev/cel/policy:parser_builder",
    "//policy/src/main/java/dev/cel/policy:compiler",
    "//policy/src/main/java/dev/cel/policy:compiler_builder",
    "//policy/src/main/java/dev/cel/policy:compiler_factory",
    "//policy/src/main/java/dev/cel/policy:policy_parser_context",
    "//policy/src/main/java/dev/cel/policy:compiled_rule",
]

V1ALPHA1_AST_TARGETS = [
    "//common/src/main/java/dev/cel/common:proto_v1alpha1_ast",
]

CANONICAL_AST_TARGETS = [
    "//common/src/main/java/dev/cel/common:proto_ast",
]

EXTENSION_TARGETS = [
    "//extensions/src/main/java/dev/cel/extensions",
    "//extensions/src/main/java/dev/cel/extensions:optional_library",
]

BUNDLE_TARGETS = [
    "//bundle/src/main/java/dev/cel/bundle:cel",
    "//bundle/src/main/java/dev/cel/bundle:environment",
    "//bundle/src/main/java/dev/cel/bundle:environment_yaml_parser",
]

ALL_TARGETS = BUNDLE_TARGETS + RUNTIME_TARGETS + COMPILER_TARGETS + EXTENSION_TARGETS + V1ALPHA1_AST_TARGETS + CANONICAL_AST_TARGETS + OPTIMIZER_TARGETS + VALIDATOR_TARGETS + POLICY_COMPILER_TARGETS

# Excluded from the JAR as their source of truth is elsewhere
EXCLUDED_TARGETS = [
    "@com_google_googleapis//google/api/expr/v1alpha1:expr_java_proto",
]

JAVA_DOC_OPTIONS = [
    "-Xdoclint:none",
    "--ignore-source-errors",
]

pom_file(
    name = "cel_pom",
    substitutions = {
        "CEL_VERSION": CEL_VERSION,
        "CEL_ARTIFACT_ID": "cel",
        "PACKAGE_NAME": "CEL Java",
        "PACKAGE_DESC": "Common Expression Language for Java. This include both the compilation and runtime packages.",
    },
    targets = ALL_TARGETS,
    template_file = "pom_template.xml",
)

java_export(
    name = "cel",
    deploy_env = EXCLUDED_TARGETS,
    javadocopts = JAVA_DOC_OPTIONS,
    maven_coordinates = "dev.cel:cel:%s" % CEL_VERSION,
    pom_template = ":cel_pom",
    runtime_deps = ALL_TARGETS,
)

pom_file(
    name = "cel_compiler_pom",
    substitutions = {
        "CEL_VERSION": CEL_VERSION,
        "CEL_ARTIFACT_ID": "compiler",
        "PACKAGE_NAME": "CEL Java Compiler",
        "PACKAGE_DESC": "Common Expression Language Compiler for Java",
    },
    targets = COMPILER_TARGETS,
    template_file = "pom_template.xml",
)

java_export(
    name = "cel_compiler",
    deploy_env = EXCLUDED_TARGETS,
    javadocopts = JAVA_DOC_OPTIONS,
    maven_coordinates = "dev.cel:compiler:%s" % CEL_VERSION,
    pom_template = ":cel_compiler_pom",
    runtime_deps = COMPILER_TARGETS,
)

pom_file(
    name = "cel_runtime_pom",
    substitutions = {
        "CEL_VERSION": CEL_VERSION,
        "CEL_ARTIFACT_ID": "runtime",
        "PACKAGE_NAME": "CEL Java Runtime",
        "PACKAGE_DESC": "Common Expression Language Runtime for Java",
    },
    targets = RUNTIME_TARGETS,
    template_file = "pom_template.xml",
)

java_export(
    name = "cel_runtime",
    deploy_env = EXCLUDED_TARGETS,
    javadocopts = JAVA_DOC_OPTIONS,
    maven_coordinates = "dev.cel:runtime:%s" % CEL_VERSION,
    pom_template = ":cel_runtime_pom",
    runtime_deps = RUNTIME_TARGETS,
)

pom_file(
    name = "cel_v1alpha1_pom",
    substitutions = {
        "CEL_VERSION": CEL_VERSION,
        "CEL_ARTIFACT_ID": "v1alpha1",
        "PACKAGE_NAME": "CEL Java v1alpha1 Utility",
        "PACKAGE_DESC": "Common Expression Language Utility for supporting v1alpha1 protobuf definitions",
    },
    targets = V1ALPHA1_AST_TARGETS,
    template_file = "pom_template.xml",
)

java_export(
    name = "cel_v1alpha1",
    deploy_env = EXCLUDED_TARGETS,
    javadocopts = JAVA_DOC_OPTIONS,
    maven_coordinates = "dev.cel:v1alpha1:%s" % CEL_VERSION,
    pom_template = ":cel_v1alpha1_pom",
    runtime_deps = V1ALPHA1_AST_TARGETS,
)

pom_file(
    name = "cel_protobuf_pom",
    substitutions = {
        "CEL_VERSION": CEL_VERSION,
        "CEL_ARTIFACT_ID": "protobuf",
        "PACKAGE_NAME": "CEL Java Protobuf adapter",
        "PACKAGE_DESC": "Common Expression Language Adapter for converting canonical cel.expr protobuf definitions",
    },
    targets = CANONICAL_AST_TARGETS,
    template_file = "pom_template.xml",
)

java_export(
    name = "cel_protobuf",
    deploy_env = EXCLUDED_TARGETS,
    javadocopts = JAVA_DOC_OPTIONS,
    maven_coordinates = "dev.cel:protobuf:%s" % CEL_VERSION,
    pom_template = ":cel_protobuf_pom",
    runtime_deps = CANONICAL_AST_TARGETS,
)

pom_file(
    name = "cel_runtime_android_pom",
    substitutions = {
        "CEL_VERSION": CEL_VERSION,
        "CEL_ARTIFACT_ID": "runtime-android",
        "PACKAGE_NAME": "CEL Java Runtime for Android",
        "PACKAGE_DESC": "Common Expression Language Lite Runtime for Java (Suitable for Android)",
    },
    targets = LITE_RUNTIME_TARGETS,
    template_file = "pom_template.xml",
)

java_export(
    name = "cel_runtime_android",
    deploy_env = EXCLUDED_TARGETS,
    javadocopts = JAVA_DOC_OPTIONS,
    maven_coordinates = "dev.cel:runtime-android:%s" % CEL_VERSION,
    pom_template = ":cel_runtime_android_pom",
    runtime_deps = LITE_RUNTIME_TARGETS,
)
